package com.yl.zqy.common;

import com.yl.zqy.enums.ResultCode;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;

/**
 * @Description 自定义异常处理类
 * @Author liang.yu
 * @Date 2024/3/24 11:45
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class CustomException extends RuntimeException implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 返回处理消息
     */
    @Schema(description = "返回处理消息")
    private final String message;

    /**
     * 返回代码
     */
    @Schema(description = "返回代码")
    private final Integer code;

    /**
     * 时间戳
     */
    @Schema(description = "时间戳")
    private final long timestamp = System.currentTimeMillis();

    public CustomException(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public CustomException(Integer code, String message, Exception e) {
        super(e);
        this.code = code;
        this.message = message;
    }

    public CustomException(ResultCode resultCode, String message) {
        this.code = resultCode.getCode();
        this.message = message;
    }

    public CustomException(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.message = analysisStringArray(resultCode.getMessage());
    }

    public String analysisStringArray(String[] strArray) {
        StringBuilder result = new StringBuilder();
        for (String s : strArray) {
            result.append(s).append(" ");
        }
        return result.toString();
    }
}
